/*
    Copyright 2011 Peter Pratscher
 
    This file is part of Surveillance.NET.

    Surveillance.NET is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    any later version.

    Surveillance.NET is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Surveillance.NET. If not, see <http://www.gnu.org/licenses/>.
*/

using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System;
using System.Runtime.Serialization;

namespace SurveillanceDotNet.Model.RegularActions
{
    [Serializable]
    public class FTPPictureUploadRegularAction : RegularAction
    {
        public bool UseAuthentication { get; set; }
        public string FtpUrl { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }

        public FTPPictureUploadRegularAction(string ftpURL)
            : base()
        {
            UseAuthentication = false;
            FtpUrl = ftpURL;
        }

        public FTPPictureUploadRegularAction(string ftpURL, string username, string password)
            : base()
        {
            UseAuthentication = true;
            FtpUrl = ftpURL;
            UserName = username;
            Password = password;

            Status = "Not Active";
        }

        protected override void Action()
        {
            foreach (SurveillanceCamera camera in _Database.Cameras)
            {
                FtpWebRequest request = (FtpWebRequest)FtpWebRequest.Create(FtpUrl);

                if (UseAuthentication)
                    request.Credentials = new NetworkCredential(UserName, Password);

                request.Method = WebRequestMethods.Ftp.UploadFile;
                request.UsePassive = true;
                request.KeepAlive = false;

                Image bmp = camera.CurrentFrame;
                MemoryStream stream = new MemoryStream();
                bmp.Save(stream, ImageFormat.Jpeg);
                stream.Position = 0;

                byte[] buffer = new byte[stream.Length];
                stream.Read(buffer, 0, buffer.Length);
                stream.Close();

                using (Stream reqStream = request.GetRequestStream())
                {
                    reqStream.Write(buffer, 0, buffer.Length);
                }
            }
        }

        #region ISerializable Implementation
        protected FTPPictureUploadRegularAction(SerializationInfo info, StreamingContext context)
            : base(info, context)
        {
            UseAuthentication = info.GetBoolean("UseAuthentication");
            FtpUrl = info.GetString("FtpUrl");
            UserName = info.GetString("UserName");
            Password = info.GetString("Password");

            Status = "Not active";
        }

        public override void GetObjectData(SerializationInfo info, StreamingContext context)
        {
            base.GetObjectData(info, context);
            info.AddValue("UseAuthentication", UseAuthentication);
            info.AddValue("FtpUrl", FtpUrl);
            info.AddValue("UserName", UserName);
            info.AddValue("Password", Password);
        }
        #endregion
    }
}
